package com.compass.doctor.ui.patient.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.framework.ext.stringListToJson
import com.compass.framework.model.CreateHospitalized
import com.compass.framework.model.CreateInspection
import com.compass.framework.model.CreateInspectionWarp
import com.compass.framework.model.Diagnosis
import com.compass.framework.model.InpatientWard
import com.compass.framework.model.InspectionBean
import com.compass.framework.toast.TipsToast
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description： 创建检查/检验 住院证 viewModel
 * Date:2024/1/30 17:25
 */
class ExamineViewModel : BaseViewModel() {
    val inpatientLiveData = MutableLiveData<MutableList<InpatientWard>?>()
    val conditionLiveData = MutableLiveData<MutableList<InpatientWard>?>()//查询入院情况
    val inspectionLiveData = MutableLiveData<MutableList<Diagnosis>?>()//检查项目
    val sampleBodyLiveData = MutableLiveData<MutableList<Diagnosis>?>()//查询标本和部位
    val departmentsLiveData = MutableLiveData<MutableList<InpatientWard>?>()//检验检测执行科室
    val massLiveData = MutableLiveData<Boolean>()//群发赠送成功
    val createHospitalizedLiveData = MutableLiveData<String>()//住院证创建成功
    val createInspectionLiveData = MutableLiveData<Any?>()//检验检查创建成功



    /**
     * 医生向患者群发消息
     */
    fun massMessaging(sendingTime:Long,patientIds:ArrayList<String>,message:String,giveFlag:Int,giveNumber:String?,giveTime:String?) {
        val params = JsonObject().apply {
            addProperty("sendingTime",sendingTime)
            add("patientIdList", stringListToJson(patientIds))
            addProperty("message",message)
            addProperty("giveNumber",giveNumber)
            addProperty("giveTime",giveTime)
            addProperty("giveFlag",giveFlag)
            addProperty("type","2")
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.massMessaging(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }) {
            it?.let {
                massLiveData.value=true
            }

        }
    }



    /**
     *检查检验执行科室
     */
    fun getInspectionDepartments(id: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInspectionDepartments(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                departmentsLiveData.value = null
            }
        }) {
            departmentsLiveData.value = it


        }
    }


    /**
     *搜索科室
     */
    fun searchDepartment(name: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.searchDepartment(name)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                departmentsLiveData.value = null
            }
        }) {
            it?.let {
                departmentsLiveData.value = it
            }?:kotlin.run {
                departmentsLiveData.value = null
            }

        }
    }


    /**
     *检查检验列表
     */
    fun getInspectionItem(keyword: String = "", type: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInspectionItem(keyword, type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                inspectionLiveData.value = null
            }
        }) {
            inspectionLiveData.value = it
        }
    }

    /**
     *检验项目
     */
    fun getSampleBody(type: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getSampleBody(type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                sampleBodyLiveData.value = null
            }
        }) {
            sampleBodyLiveData.value = it
        }
    }

    /**
     * 查询病区
     */
    fun getInpatientWardList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInpatientWardList()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                inpatientLiveData.value = null
            }
        }) {
            inpatientLiveData.value = it
        }
    }

    /**
     * 查询入院情况
     */
    fun getConditionList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConditionList()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                conditionLiveData.value=null
            }
        }) {
            conditionLiveData.value=it
        }
    }


    /**
     * 开具住院证
     */
    fun createHospitalized(createHospitalized: CreateHospitalized) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.createHospitalized(createHospitalized)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }) {
            createHospitalizedLiveData.value=it
        }
    }


    /**
     * 开检查检验
     */
    fun createInspection(createInspection: CreateInspectionWarp) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.createInspection(createInspection)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                createInspectionLiveData.value=null
            }
        }) {
            createInspectionLiveData.value=it
        }
    }
}